package com.efesco.payroll.employeeSalary;

import com.bstek.dorado.module.AbstractModule;
import com.bstek.dorado.common.DoradoContext;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.efesco.common.Tools;

/**
 * employeeSalaryModule
 */
public class employeeSalaryModule
    extends AbstractDatasetListener {
public boolean beforeLoadData(Dataset dataset) throws Exception { 
		DoradoContext context = DoradoContext.getContext();
		SqlDataset selectEmployee=( SqlDataset)dataset;
	    String classId=(String)context.getAttribute(context.SESSION,"PayrollClassId");
	    String companyId=(String)context.getAttribute(context.SESSION,"PayrollComId");
	    String employeeCode=Tools.toStr(selectEmployee.parameters().getString("employeeCode"));
	    String employeeName=Tools.toStr(selectEmployee.parameters().getString("employeeName"));
	    String idCardNum=Tools.toStr(selectEmployee.parameters().getString("idCardNum"));
	    String deptId=Tools.toStr(selectEmployee.parameters().getString("deptId"));
	    String otherClass=Tools.toStr(selectEmployee.parameters().getString("otherClass"));

      
       String sql="select distinct(s_employee.EMPLOYEE_ID) EMPLOYEE_ID,s_employee.EMPLOYEE_NAME,"
    	  + "(select bc.EMPLOYEE_BANK_CARD_ID "
    	  + "from s_employee_bankcard bc,s_employee_bankcard_busi bcb "
          + "where bc.employee_bank_card_id=bcb.employee_bank_card_id(+) and bc.employee_id=s_employee.employee_id and bcb.busi_type=1 and rownum=1) EMPLOYEE_BANK_CARD_ID,"
    	  + "(select bc.bank_name "
    	  + "from s_employee_bankcard bc,s_employee_bankcard_busi bcb "
          + "where bc.employee_bank_card_id=bcb.employee_bank_card_id(+) and bc.employee_id=s_employee.employee_id and bcb.busi_type=1 and rownum=1) bank_name,"
          
          + "(select bc.bank_account "
    	  + "from s_employee_bankcard bc,s_employee_bankcard_busi bcb "
          + "where bc.employee_bank_card_id=bcb.employee_bank_card_id(+) and bc.employee_id=s_employee.employee_id and bcb.busi_type=1 and rownum=1) bank_account,"
          
          + "(select bc.PROVINCE_NAME "
    	  + "from s_employee_bankcard bc,s_employee_bankcard_busi bcb "
          + "where bc.employee_bank_card_id=bcb.employee_bank_card_id(+) and bc.employee_id=s_employee.employee_id and bcb.busi_type=1 and rownum=1) PROVINCE_NAME,"
          
          + "(select bc.CITY_NAME "
    	  + "from s_employee_bankcard bc,s_employee_bankcard_busi bcb "
          + "where bc.employee_bank_card_id=bcb.employee_bank_card_id(+) and bc.employee_id=s_employee.employee_id and bcb.busi_type=1 and rownum=1) CITY_NAME,"
          
          + "(select bc.BANK_TYPE "
    	  + "from s_employee_bankcard bc,s_employee_bankcard_busi bcb "
          + "where bc.employee_bank_card_id=bcb.employee_bank_card_id(+) and bc.employee_id=s_employee.employee_id and bcb.busi_type=1 and rownum=1) BANK_TYPE "
          
          + "from s_employee,s_wa_psn s_wa_psn,s_dept_employee s_dept_employee,s_employee_on_off_post eoop "          
          + "where eoop.employee_id=s_employee.employee_id and s_employee.employee_id = s_wa_psn.employee_id(+) and "
          + "s_employee.employee_id = s_dept_employee.employee_id(+) and "         
          + "eoop.SINGLE_COMPANY_ID = "+companyId 
          + "and not exists(select 's' from s_wa_psn where employee_id=s_employee.employee_id and dr=0 and wa_class_id = "+classId+")";

      if (employeeCode != null )  {
        if (!employeeCode.equals(""))
          sql=sql+" and s_employee.employee_code like '%"+employeeCode+"%' ";
      }
      if (employeeName != null )  {
        if (!employeeName.equals(""))  {
          employeeName=new String(employeeName.getBytes ("8859_1"),"GBK");
          sql=sql+" and s_employee.employee_name like '%"+employeeName+"%' ";
        }
      }
      if (idCardNum != null )  {
        if (!idCardNum.equals(""))
        sql=sql+" and s_employee.id_card_num = "+idCardNum;
      }
      if (deptId != null )  {
        if (!deptId.equals(""))
        sql=sql+" and s_dept_employee.dept_id = "+deptId ;
      }
      if (otherClass != null )  {
        if (otherClass.equals("0")) {
          sql=sql+" and ((s_wa_psn.DR = 1 and s_wa_psn.wa_class_id = "+classId+" ) or (s_wa_psn.DR is null)) ";
        }
        if (otherClass.equals("1")) {
          sql=sql+" and (s_wa_psn.DR = 0 and s_wa_psn.wa_class_id <> "+classId+" ) ";
        }
      }
      else  {
        sql=sql+" and (";
        sql=sql+"((s_wa_psn.DR = 1 and s_wa_psn.wa_class_id = "+classId+" ) or (s_wa_psn.DR is null))";
        sql=sql+" or ";
        sql=sql+"(s_wa_psn.DR = 0 and s_wa_psn.wa_class_id <> "+classId+")";
        sql=sql+" )";
      }
      System.out.println(sql);
      selectEmployee.setSql(sql);

      return true;
  }

}
